package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.*;
import com.itheima.entity.SysRole;
import com.itheima.entity.SysUser;
import com.itheima.mapper.SysUserMapper;
import com.itheima.service.SysRoleService;
import com.itheima.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
@Autowired
private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    public SysUserDtoes get(String id) {
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.eq(SysUser::getId, id);
        SysUser one = super.getOne(qw);
        String roleId = one.getRoleId();
        roleId = roleId.replace("\"","");
        roleId = roleId.replace("[", "");
        roleId = roleId.replace("]", "");
        List<String> list = Arrays.asList(roleId.split(","));
        SysUserDtoes sysUserDtoes = new SysUserDtoes();
        BeanUtils.copyProperties(one, sysUserDtoes);
        sysUserDtoes.setRole_id(list);
        return sysUserDtoes;
    }

//    @Override
//    public SysUserPageDto list(Integer page, Integer limit, String login_name, String phone, Integer status) {
//        LambdaQueryWrapper<SysUser>qw=new LambdaQueryWrapper<>();
//        qw.eq(login_name!=null,SysUser::getLoginName,login_name);
//        SysUser one = super.getOne(qw);
//        System.out.println(one.toString());
//        return null;
//    }
    @Override
    public PageDto pageList(String login_name, Integer status, String phone, int page, int limit) {
//        Page<SysUser> pageInfo=new Page<>(page,limit);
//        Page<SysUserDto>page1=new Page<>();
//        LambdaQueryWrapper<SysUser>qw=new LambdaQueryWrapper<>();
//        qw.like(login_name!=null,SysUser::getLoginName,login_name);
//        qw.like(phone!=null,SysUser::getPhone,phone);
//        qw.like(status!=null,SysUser::getStatus,status);
//        BeanUtils.copyProperties(pageInfo,page1,"records");
//        List<SysUser> records = pageInfo.getRecords();
//        List<SysUserDto> list = new ArrayList<>();
//        for (SysUser record : records) {
//            SysUserDto userDto = new SysUserDto();
//            BeanUtils.copyProperties(record,userDto);
//            String id =record.getId();
//            SysUser byId = sysUserService.getById(id);
//            if(byId!=null){
//                userDto.setLoginName( byId.getLoginName());
//                userDto.setPhone(byId.getPhone());
//                userDto.setStatus(byId.getStatus());
//                list.add(userDto);
//            }
//        }
//        page1.setRecords(list);
//        PageDto pageDto = new PageDto();
//        BeanUtils.copyProperties(page1,pageDto);
//        return pageDto;
//    }
        Page<SysUser> pageInfo = new Page(page, limit);
        Page<SysUserRoleDto> dtoPage = new Page<>();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(login_name), SysUser::getLoginName, login_name);
        queryWrapper.like(StringUtils.isNotEmpty(phone), SysUser::getPhone, phone);
        queryWrapper.like(status != null, SysUser::getStatus, status);
        super.page(pageInfo, queryWrapper);
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<SysUser> records = pageInfo.getRecords();
        List<SysUserRoleDto> list = records.stream().map((item) -> {
            SysUserRoleDto dto = new SysUserRoleDto();
            BeanUtils.copyProperties(item, dto);
            String role_id = item.getRoleId();

            System.out.println(role_id);
            role_id = role_id.replace("\"", "");
            role_id = role_id.replace("[", "");
            role_id = role_id.replace("]", "");

            String[] split = role_id.split(", "); //有空格
            String str = "";
            for (String s : split) {
                SysRole sysRole = sysRoleService.getById(s);
                if (ObjectUtils.isNotEmpty(sysRole)) {
                    str = str + sysRole.getName() + ",";
                }
            }
            String[] rolename = str.split(",");
            dto.setRolename(Arrays.asList(rolename));
            return dto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        PageDto pageDto = new PageDto();
        pageDto.setCurrentPage(pageInfo.getCurrent());
        pageDto.setPageSize(pageInfo.getSize());
        pageDto.setCount(pageInfo.getTotal());
        pageDto.setTotalPages((long) page);
        pageDto.setData(dtoPage.getRecords());
        super.page(pageInfo, queryWrapper);
        return pageDto;
}}
